/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.modules.admin.entity.ElevatorFloorStandTemplate;
import org.springblade.modules.admin.vo.ElevatorFloorStandTemplateTreeVO;
import org.springblade.modules.admin.vo.ElevatorFloorStandTemplateVO;
import org.springblade.modules.admin.mapper.ElevatorFloorStandTemplateMapper;
import org.springblade.modules.admin.service.IElevatorFloorStandTemplateService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.admin.vo.elevator.ElevatorFloorVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 电梯层站结构表 服务实现类
 *
 * @author ysq
 * @since 2024-10-11
 */
@Service
public class ElevatorFloorStandTemplateServiceImpl extends BaseServiceImpl<ElevatorFloorStandTemplateMapper, ElevatorFloorStandTemplate> implements IElevatorFloorStandTemplateService {

	@Override
	public IPage<ElevatorFloorStandTemplateVO> selectElevatorFloorStandTemplatePage(IPage<ElevatorFloorStandTemplateVO> page, ElevatorFloorStandTemplateVO elevatorFloorStandTemplate) {
		return page.setRecords(baseMapper.selectElevatorFloorStandTemplatePage(page, elevatorFloorStandTemplate));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void syncElevatorFloorStandTemplate(Long elevatorId, List<ElevatorFloorVo> floorPlatforms, List<ElevatorFloorVo> floorDoors) {
		List<ElevatorFloorStandTemplate> oldList = list(Wrappers.<ElevatorFloorStandTemplate>lambdaQuery().eq(ElevatorFloorStandTemplate::getElevatorId, elevatorId));
		List<Long> removeIds = oldList.stream().map(ElevatorFloorStandTemplate::getId).collect(Collectors.toList());
		if (!removeIds.isEmpty()) {
			removeByIds(removeIds);
		}

		int sort = 0;
		// 1、先插入层站信息
		List<ElevatorFloorStandTemplate> insertFloorList = new ArrayList<>();
		for (ElevatorFloorVo floorPlatform : floorPlatforms) {
			if (floorPlatform.getIsHas()) {
				ElevatorFloorStandTemplate elevatorFloorStandTemplate = new ElevatorFloorStandTemplate();
				elevatorFloorStandTemplate.setElevatorId(elevatorId);
				elevatorFloorStandTemplate.setName(floorPlatform.getFloorNum());
				elevatorFloorStandTemplate.setFlag(1);
				elevatorFloorStandTemplate.setSort(sort++);
				insertFloorList.add(elevatorFloorStandTemplate);
			}
		}
		if (insertFloorList.isEmpty()) {
			return;
		}
		saveBatch(insertFloorList);

		// 2、再插入门厅信息
		Map<String, Long> floorIdMap = insertFloorList.stream().collect(Collectors.toMap(ElevatorFloorStandTemplate::getName, ElevatorFloorStandTemplate::getId));
		List<ElevatorFloorStandTemplate> insertDoorList = new ArrayList<>();
		for (ElevatorFloorVo floorDoor : floorDoors) {
			if (floorIdMap.containsKey(floorDoor.getFloorNum())) {
				if (floorDoor.getIsFrontDoor()) {
					ElevatorFloorStandTemplate frontDoor = new ElevatorFloorStandTemplate();
					frontDoor.setParentId(floorIdMap.get(floorDoor.getFloorNum()));
					frontDoor.setParentName(floorDoor.getFloorNum());
					frontDoor.setElevatorId(elevatorId);
					frontDoor.setName("前门");
					frontDoor.setFlag(2);
					frontDoor.setSort(sort++);
					insertDoorList.add(frontDoor);
				}

				if (floorDoor.getIsBackDoor()) {
					ElevatorFloorStandTemplate backDoor = new ElevatorFloorStandTemplate();
					backDoor.setParentId(floorIdMap.get(floorDoor.getFloorNum()));
					backDoor.setParentName(floorDoor.getFloorNum());
					backDoor.setElevatorId(elevatorId);
					backDoor.setName("后门");
					backDoor.setFlag(2);
					backDoor.setSort(sort++);
					insertDoorList.add(backDoor);
				}
			}
		}
		if (!insertDoorList.isEmpty()) {
			saveBatch(insertDoorList);
		}
	}

	@Override
	public List<ElevatorFloorStandTemplateTreeVO> getTree(Long elevatorId) {
		return baseMapper.getTree(elevatorId);
	}

	@Override
	public List<ElevatorFloorStandTemplateVO> detailList(Long elevatorId, Long partsId, Integer flag) {
		return baseMapper.detailList(elevatorId, partsId, flag);
	}

	@Override
	public List<ElevatorFloorStandTemplateTreeVO> getListByElevatorIdAndPartsId(Long elevatorId, Long partsId) {
		return baseMapper.getListByElevatorIdAndPartsId(elevatorId, partsId);
	}
}
